/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.tabs.controllers;

import java.awt.image.BufferedImage;
import java.util.Observable;
import systole.domain.analysis.Analysis;
import systole.domain.report.Report;

/**
 *
 * @author jmj
 */
public class ReportModel extends Observable {

    private boolean dirtyModel = false;
    private Report originalReport;
    private Report currentReport;

    /**
     * 
     * @param analysis
     */
    public ReportModel(Analysis analysis) {
        this.originalReport = analysis.getReport();
        this.currentReport = new Report();
        if (this.originalReport != null) {
            this.currentReport.setComments(this.originalReport.getComments());
            this.currentReport.setCommentAOD(this.originalReport.getCommentAOD());
            this.currentReport.setCommentAOS(this.originalReport.getCommentAOS());
            this.currentReport.setCommentIAR(this.originalReport.getCommentIAR());
            this.currentReport.setConclusion(this.originalReport.getConclusion());
            this.currentReport.setBackgroundFamily(this.originalReport.getBackgroundFamily());
            this.currentReport.setBackgroundMedicines(this.originalReport.getBackgroundMedicines());
            this.currentReport.setBackgroundPathologies(this.originalReport.getBackgroundPathologies());
            this.currentReport.setBackgroundSurgeries(this.originalReport.getBackgroundSurgeries());
            this.currentReport.setHabitsSummary(this.originalReport.getHabitsSummary());
        }
    }

    /**
     * @return the dirtyModel
     */
    public boolean isDirtyModel() {
        return dirtyModel;
    }

    /**
     *
     * @param comments
     */
    public void updateComments(String comments) {
        this.currentReport.setComments(comments);
        this.markAsDirty();
        this.setChanged();
        this.notifyObservers(this.currentReport);
    }

    /**
     *
     * @param conclusion
     */
    public void updateConclusion(String conclusion) {
        this.currentReport.setConclusion(conclusion);
        this.markAsDirty();
        this.setChanged();
        this.notifyObservers(this.currentReport);
    }

    /**
     *
     * @param commentAOD
     * @param commentAOS
     * @param commentIAR
     */
    public void updateParamsComments(String commentAOD, String commentAOS, String commentIAR) {
        this.currentReport.setCommentAOD(commentAOD);
        this.currentReport.setCommentAOS(commentAOS);
        this.currentReport.setCommentIAR(commentIAR);
        this.markAsDirty();
        this.setChanged();
        this.notifyObservers(this.currentReport);
    }

    /**
     * 
     * @param backgroundSurgeries
     * @param backgroundFamily
     * @param backgroundPathologies
     * @param backgroundMedicines
     * @param habitsSummary
     */
    public void updateBackground(String backgroundSurgeries,
            String backgroundFamily, String backgroundPathologies,
            String backgroundMedicines, String habitsSummary) {
        this.currentReport.setBackgroundFamily(backgroundFamily);
        this.currentReport.setBackgroundMedicines(backgroundMedicines);
        this.currentReport.setBackgroundPathologies(backgroundPathologies);
        this.currentReport.setBackgroundSurgeries(backgroundSurgeries);
        this.currentReport.setHabitsSummary(habitsSummary);
        this.markAsDirty();
        this.setChanged();
        this.notifyObservers(this.currentReport);
    }

    /**
     *
     * @param image
     */
    public void updatePlotImage(BufferedImage image) {
        this.currentReport.setGraphic(image);
        this.setChanged();
        this.notifyObservers(this.currentReport);
    }

    public void save() {
        if (this.isDirtyModel()) {
            if (this.originalReport == null) {
                this.originalReport = new Report();
            }
            this.originalReport.setComments(this.currentReport.getComments());
            this.originalReport.setCommentAOD(this.currentReport.getCommentAOD());
            this.originalReport.setCommentAOS(this.currentReport.getCommentAOS());
            this.originalReport.setCommentIAR(this.currentReport.getCommentIAR());
            this.originalReport.setConclusion(this.currentReport.getConclusion());
            this.originalReport.setBackgroundFamily(this.currentReport.getBackgroundFamily());
            this.originalReport.setBackgroundMedicines(this.currentReport.getBackgroundMedicines());
            this.originalReport.setBackgroundPathologies(this.currentReport.getBackgroundPathologies());
            this.originalReport.setBackgroundSurgeries(this.currentReport.getBackgroundSurgeries());
            this.originalReport.setHabitsSummary(this.currentReport.getHabitsSummary());
            this.originalReport.setGraphic(this.currentReport.getGraphic());
            this.dirtyModel = false;
        }
    }

    /**
     * @return the originalReport
     */
    public Report getOriginalReport() {
        return originalReport;
    }

    /**
     * @return the currentReport
     */
    public Report getCurrentReport() {
        return currentReport;
    }

    private void markAsDirty() {
        this.dirtyModel = true;
    }
}
